package com.cy.pj.modules.service.impl;

import com.cy.pj.common.bo.PageObject;
import com.cy.pj.modules.entity.Brand;
import com.cy.pj.modules.mapper.BrandMapper;
import com.cy.pj.modules.service.IBrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 品牌商Service业务层处理
 *
 * @author kiven
 * @date 2023-02-09
 */
@Service
public class BrandServiceImpl implements IBrandService
{
    @Autowired
    private BrandMapper brandMapper;

    @Value("${fz.pageHelper.pageSize}")
    private Integer pageSize;
    /**
     * 查询品牌商
     *
     * @param id 品牌商ID
     * @return 品牌商
     */
    @Override
    public Brand selectBrandById(Long id)
    {
        return brandMapper.selectBrandById(id);
    }

    /**
     * 查询品牌商列表
     *
     * @param brand 品牌商
     * @return 品牌商
     */
    @Override
    public PageObject<Brand> selectBrandList(Brand brand, Integer pageCurrent)
    {
        //1.参数校验
        if(pageCurrent==null||pageCurrent<1) {
            throw new IllegalArgumentException("页码值无效");
        }
        //2.设置分页参数
        String orderBy = "add_time desc";
        Page<Brand> page= PageHelper.startPage(pageCurrent,pageSize,orderBy);
        //3.查询当前页记录
        List<Brand> list= brandMapper.selectBrandList(brand);
        return new PageObject<>(list, (int)page.getTotal(), page.getPages(), pageSize, pageCurrent);
    }

    public List<Brand> selectBrandList1()
    {
        return brandMapper.selectBrandList1();
    }

    /**
     * 新增品牌商
     *
     * @param brand 品牌商
     * @return 结果
     */
    @Override
    public int insertBrand(Brand brand)
    {
        brand.setAddTime(new Date());
        return brandMapper.insertBrand(brand);
    }

    /**
     * 修改品牌商
     *
     * @param brand 品牌商
     * @return 结果
     */
    @Override
    public int updateBrand(Brand brand)
    {
        brand.setUpdateTime(new Date());
        return brandMapper.updateBrand(brand);
    }

    /**
     * 删除品牌商对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBrandByIds(String ids)
    {
        String[] ids1=ids.split(",");
        return brandMapper.deleteBrandByIds(ids1);
    }

    /**
     * 删除品牌商信息
     *
     * @param id 品牌商ID
     * @return 结果
     */
    @Override
    public int deleteBrandById(Long id)
    {
        return brandMapper.deleteBrandById(id);
    }
}
